#include "PhysicalBody.h"
PhysicalBody::PhysicalBody(){
	SetX(0);
	SetY(0);
	SetW(0);
	SetH(0);
	SetVX(0);
	SetVY(0);
	SetAX(0);
	SetAY(0);
	colliding = false;
	drag.Set(0.2,0.2);
}
PhysicalBody::~PhysicalBody(){}
void PhysicalBody::Update(){

	//velocity -= velocity * drag
	float vx = velocity.GetX();
	float vy = velocity.GetY();

	//lower limit of velocity
	float limit = 0.1f;
	
	if(vx > limit || vx < -limit) vx -= vx * drag.GetX();
	else vx = 0;
		
	if(vy > limit || vy < -limit) vy -= vy * drag.GetY();
	else vy = 0;



	velocity.SetX(vx);
	velocity.SetY(vy);
	
	
	//position += velocity
	SetX((int)vx + GetX());
	SetY((int)vy + GetY());
	

}
void PhysicalBody::SetX(int x){
	dimensions.SetX(x);
}
void PhysicalBody::SetY(int y){
	dimensions.SetY(y);
}
void PhysicalBody::SetW(int w){
	dimensions.SetW(w);
}
void PhysicalBody::SetH(int h){
	dimensions.SetH(h);
}
void PhysicalBody::SetVX(float x){
	velocity.SetX(x);
}
void PhysicalBody::SetVY(float y){
	velocity.SetY(y);
}
void PhysicalBody::SetAX(float x){
	acceleration.SetX(x);
}
void PhysicalBody::SetAY(float y){
	acceleration.SetY(y);
}
int PhysicalBody::GetX(){
	return dimensions.GetX();
}
int PhysicalBody::GetY(){
	return dimensions.GetY();
}
int PhysicalBody::GetW(){
	return dimensions.GetW();
}
int PhysicalBody::GetHalfOfWidth(){
	return dimensions.GetHalfOfWidth();
}
int PhysicalBody::GetH(){
	return dimensions.GetH();
}
int PhysicalBody::GetHalfOfHeight(){
	return dimensions.GetHalfOfHeight();
}
float PhysicalBody::GetVX(){
	return velocity.GetX();
}
float PhysicalBody::GetVY(){
	return velocity.GetY();
}
float PhysicalBody::GetAX(){
	return acceleration.GetX();
}
float PhysicalBody::GetAY(){
	return acceleration.GetY();
}
Rectangle* PhysicalBody::GetDimensions(){
	return &dimensions;
}
void PhysicalBody::Blit(){
	if(colliding)
		LibraryInterface::DrawFilledRect(GetX(), GetY(), GetW()/2, GetH()/2, 255, 0, 0);
	else
		LibraryInterface::DrawFilledRect(GetX(), GetY(), GetW()/2, GetH()/2, 0, 125, 255);
}
bool PhysicalBody::DetectCollisionWith(PhysicalBody* other_body){
	Rectangle *rect1, *rect2;
	rect1 = GetDimensions();
	rect2 = other_body->GetDimensions();
	return Rectangle::AreRectanglesIntersecting(rect1, rect2);
}
int PhysicalBody::CollideWith(PhysicalBody* other_body){
	if(DetectCollisionWith(other_body)){
		other_body->SetCollisionStatus(true);
		SetCollisionStatus(true);
		return true;	
	}
	else
		return false;
}
void PhysicalBody::ApplyForce(float x, float y){
	velocity.SetX(velocity.GetX() + x);
	velocity.SetY(velocity.GetY() + y);
}
void PhysicalBody::SetCollisionStatus(bool colliding){
	this->colliding = colliding;
}
bool PhysicalBody::GetCollisionStatus(){
	return colliding;
}